package ventanas;
/*
import javax.swing.JOptionPane;
import beans.Articulosdocumento;

public class Ventana_Editar_Item extends javax.swing.JDialog {

    /** Creates new form Ventana_Editar_Item
    Articulosdocumento ad;
    boolean sw = false;
    int tipo;

    public Ventana_Editar_Item(java.awt.Frame parent, boolean modal, Articulosdocumento ad, int tipo) {
        super(parent, modal);
        this.ad = ad;
        double existencia = ad.getArticulo().getExistencia().longValue();
        double cantida = ad.getCantidad();
        if (tipo == 0)//compras
        {
            existencia = existencia - cantida;
        } else//ventas
        {
            existencia = existencia + cantida;
        }
        ad.getArticulo().setExistencia((existencia));
        this.tipo = tipo;
        initComponents();
        llenar();
        cantidad.requestFocus();
        utilidades.FormatoNumeros fn = new utilidades.FormatoNumeros(vrunit);
        vrunit.addKeyListener(fn);
        vrunit.addFocusListener(fn);

        utilidades.FormatoCantidades fc = new utilidades.FormatoCantidades(cantidad);
        cantidad.addKeyListener(fc);
        cantidad.addFocusListener(fc);


        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    public void llenar() {
        articulo.setText(ad.getArticulo().getCodigobarras() + "");
        dcomercial.setText(ad.getArticulo().getDescripcioncomercial());
        cantidad.setText(ad.getCantidad() + "");
        vrunit.setText(utilidades.FormatoNumeros.formatear("" + ad.getVlrunitario()));
        vrpar.setText(utilidades.FormatoNumeros.formatear("" + ad.getVlrparcial()));
        existencia.setText(ad.getArticulo().getExistencia() + "");
    }

    public static Articulosdocumento editarItem(java.awt.Frame parent, boolean modal, Articulosdocumento ad, int tipo) {
        Ventana_Editar_Item vei = new Ventana_Editar_Item(parent, modal, ad, tipo);
        if (vei.modificado()) {
            return vei.getAd();
        }
        return null;
    }

    public Articulosdocumento getAd() {
        return ad;
    }

    public boolean modificado() {
        return sw;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        cantidad = new javax.swing.JTextField();
        vrunit = new javax.swing.JTextField();
        articulo = new javax.swing.JTextField();
        dcomercial = new javax.swing.JTextField();
        vrpar = new javax.swing.JTextField();
        guardar = new javax.swing.JButton();
        salir = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        existencia = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Editart");

        jPanel1.setBackground(new java.awt.Color(212, 233, 255));

        cantidad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cantidadActionPerformed(evt);
            }
        });
        cantidad.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                cantidadFocusGained(evt);
            }
        });
        cantidad.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                cantidadKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                cantidadKeyTyped(evt);
            }
        });

        vrunit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vrunitActionPerformed(evt);
            }
        });

        articulo.setEditable(false);

        dcomercial.setEditable(false);

        vrpar.setEditable(false);

        guardar.setBackground(new java.awt.Color(0, 153, 255));
        guardar.setFont(new java.awt.Font("Tahoma", 1, 11));
        guardar.setForeground(new java.awt.Color(0, 51, 153));
        guardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/ok.png"))); // NOI18N
        guardar.setText("Guardar");
        guardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardarActionPerformed(evt);
            }
        });

        salir.setBackground(new java.awt.Color(0, 153, 255));
        salir.setFont(new java.awt.Font("Tahoma", 1, 11));
        salir.setForeground(new java.awt.Color(0, 51, 153));
        salir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/bulletCritical.png"))); // NOI18N
        salir.setText("Salir");
        salir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                salirActionPerformed(evt);
            }
        });

        jLabel1.setText("Articulo");

        jLabel2.setText("Descripcion Comercial");

        jLabel7.setText("Vlr Par");

        jLabel6.setText("Vlr Unit");

        jLabel5.setText("Cantidad");

        existencia.setEditable(false);

        jLabel3.setText("Existencia");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(articulo)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                            .addComponent(jLabel3)
                                            .addGap(18, 18, 18)
                                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                            .addComponent(existencia, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(cantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(dcomercial, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(vrunit, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel6))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel7)
                                    .addComponent(vrpar)))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(65, 65, 65)
                        .addComponent(guardar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(salir, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(14, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dcomercial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(articulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7)
                    .addComponent(jLabel3)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(vrunit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(vrpar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(existencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(salir)
                    .addComponent(guardar))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void guardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardarActionPerformed
        // TODO add your handling code here:
        sw = false;
        if (validar()) {
            double totalparcial = Double.parseDouble(this.vrpar.getText().replaceAll(",", ""));
            double cantidades = Integer.parseInt(cantidad.getText());
            ad.setCantidad(cantidades);
            ad.setVlrunitario(Double.parseDouble(this.vrunit.getText().replaceAll(",", "")));
            ad.setVlrparcial(totalparcial);
            double existencia = ad.getArticulo().getExistencia() + cantidades;
            if (tipo == 1) {
                existencia = ad.getArticulo().getExistencia().longValue() - cantidades;
            }

            ad.getArticulo().setExistencia((existencia));
            sw = true;

            this.dispose();
        }
    }//GEN-LAST:event_guardarActionPerformed

    public boolean validar() {
        if (cantidad.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(null, "Digite la Cantidad");
            cantidad.requestFocus();
            return false;
        } else if (Double.parseDouble(cantidad.getText()) <= 0) {
            JOptionPane.showMessageDialog(null, "Digite la Cantidad");
            cantidad.requestFocus();
            return false;
        } else if (vrunit.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(null, "Digite un Valor Unitario");
            vrunit.requestFocus();
            return false;
        } else if (Double.parseDouble(vrunit.getText().replaceAll(",", "")) <= 0) {
            JOptionPane.showMessageDialog(null, "Digite un Valor Unitario");
            vrunit.requestFocus();
            return false;
        }
        return true;

    }

    private void salirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salirActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_salirActionPerformed

    private void cantidadKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cantidadKeyTyped
        // TODO add your handling code here:
        if (!Character.isDigit(evt.getKeyChar())) {
            evt.consume();
        }
    }//GEN-LAST:event_cantidadKeyTyped

    private void cantidadKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cantidadKeyReleased
        // TODO add your handling code here:
        if (tipo == 0) {//compras
        } else {//ventas
        }
    }//GEN-LAST:event_cantidadKeyReleased

    private void cantidadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cantidadActionPerformed
        // TODO add your handling code here:

        if (!cantidad.getText().trim().equals("")) {
            int cantidades = Integer.parseInt(cantidad.getText());
            if (tipo == 0) {//compras
                if (cantidad.getText().trim().equals("") && cantidades <= 0) {
                    JOptionPane.showMessageDialog(null, "Digite una CANTIDAD");
                } else {
                    //  if(ad!=null){
                    long existencia = ad.getArticulo().getExistencia().longValue();
                    if (cantidades > 0) {
                        double vltunitario = Double.parseDouble(vrunit.getText().replaceAll(",", ""));
                        double totalparcial = cantidades * (vltunitario);
                        vrpar.setText(utilidades.FormatoNumeros.formatear("" + (totalparcial)));
                        vrunit.requestFocus();
                        /* }else
                        JOptionPane.showMessageDialog(null,"La cantidad digitada es mayor a la disponibilidad o exitencia del articulo");
                      }
                }

            } else {//ventas
                long existencia = ad.getArticulo().getExistencia().longValue();

                if (cantidades <= existencia) {
                    double vltunitario = Double.parseDouble(vrunit.getText().replaceAll(",", ""));
                    double totalparcial = cantidades * (vltunitario);
                    vrpar.setText(utilidades.FormatoNumeros.formatear("" + (totalparcial)));
                    vrunit.requestFocus();
                } else {

                    JOptionPane.showMessageDialog(null, "La cantidad digitada es mayor a la disponibilidad o exitencia del articulo");
                }
            }
        }
    }//GEN-LAST:event_cantidadActionPerformed

    private void vrunitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_vrunitActionPerformed
        // TODO add your handling code here:
        if (!cantidad.getText().trim().equals("")) {
            int cantidades = Integer.parseInt(cantidad.getText());
            if (tipo == 1) {//ventas
                if (ad != null) {
                    long existencia = ad.getArticulo().getExistencia().longValue();

                    double vltunitario = Double.parseDouble(vrunit.getText().replaceAll(",", ""));
                    if (vltunitario >= ad.getArticulo().getValormin()) {
                        double totalparcial = cantidades * (vltunitario);
                        vrpar.setText(utilidades.FormatoNumeros.formatear("" + (totalparcial)));
                        guardar.requestFocus();
                    } else {
                        JOptionPane.showMessageDialog(null, "El Articulo no puede ser vendio con un valor inferior al minimo de venta");
                    }
                }
            } else {//compras
                long existencia = ad.getArticulo().getExistencia().longValue();

                double vltunitario = Double.parseDouble(vrunit.getText().replaceAll(",", ""));

                double totalparcial = cantidades * (vltunitario);
                vrpar.setText(utilidades.FormatoNumeros.formatear("" + (totalparcial)));
                guardar.requestFocus();
            }
        }
    }//GEN-LAST:event_vrunitActionPerformed

    private void cantidadFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_cantidadFocusGained
        // TODO add your handling code here:
        cantidad.setSelectionStart(0);
        cantidad.setSelectionEnd(cantidad.getText().length());
    }//GEN-LAST:event_cantidadFocusGained

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField articulo;
    private javax.swing.JTextField cantidad;
    private javax.swing.JTextField dcomercial;
    private javax.swing.JTextField existencia;
    private javax.swing.JButton guardar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton salir;
    private javax.swing.JTextField vrpar;
    private javax.swing.JTextField vrunit;
    // End of variables declaration//GEN-END:variables
}
 */